package cn.zhuama.pet.service.impl;

import cn.zhuama.basic.config.BaiduAuditUtils;
import cn.zhuama.basic.context.LoginContextUtil;
import cn.zhuama.basic.util.AjaxResult;
import cn.zhuama.basic.util.PageList;
import cn.zhuama.org.domain.Employee;
import cn.zhuama.org.mapper.EmployeeMapper;
import cn.zhuama.org.mapper.ShopMapper;
import cn.zhuama.pet.domain.Pet;
import cn.zhuama.pet.domain.PetDetail;
import cn.zhuama.pet.domain.PetOnlineAuditLog;
import cn.zhuama.pet.mapper.PetDetailMapper;
import cn.zhuama.pet.mapper.PetMapper;
import cn.zhuama.pet.mapper.PetOnlineAuditLogMapper;
import cn.zhuama.pet.query.PetQuery;
import cn.zhuama.pet.service.IPetService;
import cn.zhuama.basic.service.impl.BaseServiceImpl;
import cn.zhuama.user.domain.Logininfo;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;
    @Autowired
    private ShopMapper shopMapper;

    @Override
    public void save(Pet pet) {
        pet.setState(Pet.OFF_LINE);
       petMapper.insert(pet);
       pet.getPetDetail().setPetId(pet.getId());
       petDetailMapper.insert(pet.getPetDetail());
    }
    @Override
    @Transactional
    public void editById(Pet pet) {
        super.editById(pet);
        petDetailMapper.updateById(pet.getPetDetail());
    }


    @Override
    @Transactional
    public PetDetail detailById(Long petId) {
        return petDetailMapper.selectByPetId(petId);
    }

    @Override
    @Transactional
    public AjaxResult onsale(List<Long> ids, HttpServletRequest request) {
//上架 -不能用批量操作，有上架审核
        for (Long id : ids) {

            Pet pet = petMapper.selectOne(id);
            String auditText = pet.getName();
            Boolean textBoo = BaiduAuditUtils.TextCensor(auditText);

            String petResources = pet.getResources();
            List<Boolean> booList = new ArrayList<>();   //一个宠物，多张图片的校验
            if(!StringUtils.isEmpty(petResources)){
                String[] split = petResources.split(",");
                for (String group : split) {
                    Boolean res =  BaiduAuditUtils.ImgCensor("http://39.105.218.224"+group);
                    booList.add(res);  //booList{true true,fasle}
                }

            }

            Boolean imageBoo = !booList.contains(false);//


            Logininfo logininfo = (Logininfo) LoginContextUtil.getLoginInfoFromReq(request);
            Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());


            if (textBoo && imageBoo){

                Map<String,Object> params = new HashMap<>();
                params.put("id",id);
                params.put("onsaletime",new Date());
                petMapper.onsale(params);


                //记录审核日志
                PetOnlineAuditLog auditLog = new PetOnlineAuditLog();
                auditLog.setState(1);
                auditLog.setPetId(id);
                auditLog.setAuditId(employee.getId());
                auditLog.setNote("审核成功！");
                petOnlineAuditLogMapper.insert(auditLog);
            }else{
                //审核失败-note-当前是下架状态，如果审核失败！
                //记录审核日志
                PetOnlineAuditLog auditLog = new PetOnlineAuditLog();
                auditLog.setState(0);
                auditLog.setPetId(id);
                auditLog.setAuditId(employee.getId());
                auditLog.setNote("审核失败,宠物名称或图片不合法!!!");
                petOnlineAuditLogMapper.insert(auditLog);
                //审核失败，上架失败
                return AjaxResult.me().setSuccess(false).setMessage("上架失败！审核失败,宠物名称或图片不合法!!!");
            }
        }
        return AjaxResult.me();
    }

    @Override
    @Transactional
    public AjaxResult offsale(List<Long> ids,HttpServletRequest request) {
        //下架：修改state为0，offsaletime为当前时间 ,可以用批量操作
        Map<String,Object> params = new HashMap<>();
        params.put("ids",ids);
        params.put("offsaletime",new Date());
        petMapper.offsale(params);
        return AjaxResult.me();
    }

    @Override
    @Transactional
    public void removeById(Long id) {
        petDetailMapper.deleteByPetId(id);
        super.removeById(id);
    }
    @Override
    public PageList<Pet> queryPageForEmp(PetQuery query, HttpServletRequest request){
    Long loginInfoId =LoginContextUtil.getLoginInfoFromReq(request).getId();
    Employee employee = employeeMapper.loadByLogininfoId(loginInfoId);
    Long shopId = employee.getShop_id();
        if(shopId!=null){//说明是员工
            query.setShopId(shopId);//店长
        }
        /*获取的总条数*/
        Long count = petMapper.selectTotalCount(query);
        //query没有设置员工ID ,也没有设置店铺ID 说明是管理员{获取的分页数据}
        List<Pet> pets = petMapper.queryPageForEmp(query);
        return new PageList<>(count,pets);
    }

}
